package com.txunda.zhuohired.adapter.user.my;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.bean.user.my.MyCollectBean;

import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/8 13:21
 * 功能描述：我的收藏adapter
 * 联系方式：jiahuanchen@126.com
 */

public class MyCollectAdapter extends BaseAdapter {
    private Context context;
    private int collectStatus;
    private ViewHolder holder;
    private List<MyCollectBean.DataBean> collectList;
    /**
     * 自定义监听
     */
    private HolderClickListener mHolderClickListener;



    public MyCollectAdapter(Context context, int collectStatus,List<MyCollectBean.DataBean> collectList) {
        this.collectStatus = collectStatus;
        this.context = context;
        this.collectList = collectList;
    }

    @Override
    public int getCount() {
        return collectList.size();
    }

    @Override
    public Object getItem(int i) {
        return collectList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_my_collect, null);
            holder = new ViewHolder();
            view.setTag(holder);
            ViewUtils.inject(holder, view);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        if (collectStatus == 1) {
            holder.iv_item_collect_delete.setVisibility(View.GONE);
        } else {
            holder.iv_item_collect_delete.setVisibility(View.VISIBLE);
            holder.iv_item_collect_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mHolderClickListener.onSureHolderClick(i);
                }
            });
        }
        Glide.with(context).load(collectList.get(i).getLogo()).error(R.mipmap.icon_defult).placeholder(R.mipmap
                .icon_defult).dontAnimate().into(holder.iv_item_collect_head);
        if (TextUtils.isEmpty(collectList.get(i).getTitle())) {
            holder.tv_item_collect_name.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_collect_name.setVisibility(View.VISIBLE);
            holder.tv_item_collect_name.setText(collectList.get(i).getTitle());
        }
        if (TextUtils.isEmpty(collectList.get(i).getAddress()) && TextUtils.isEmpty(collectList.get(i)
                .getClaim_shooling()) && TextUtils.isEmpty(collectList.get(i).getClaim_experience())) {
            holder.iv_item_collect_education.setVisibility(View.INVISIBLE);
        } else {
            holder.iv_item_collect_education.setVisibility(View.VISIBLE);
            holder.iv_item_collect_education.setText(collectList.get(i).getAddress() + collectList.get(i).getClaim_shooling()
                    + collectList.get(i).getClaim_experience());
        }
        if (TextUtils.isEmpty(collectList.get(i).getName())) {
            holder.tv_item_collect_company_name.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_collect_company_name.setVisibility(View.VISIBLE);
            holder.tv_item_collect_company_name.setText(collectList.get(i).getName());
        }
        if (TextUtils.isEmpty(collectList.get(i).getPrice())) {
            holder.tv_item_collect_price.setVisibility(View.INVISIBLE);
        } else {
            holder.tv_item_collect_price.setVisibility(View.VISIBLE);
            holder.tv_item_collect_price.setText(collectList.get(i).getPrice());
        }
        String string = collectList.get(i).getWelfare();
        String[] stringArr = string.split(",");
        if (TextUtils.isEmpty(collectList.get(i).getWelfare())) {
            holder.tv_item_collect_one.setVisibility(View.GONE);
            holder.tv_item_collect_two.setVisibility(View.GONE);
            holder.tv_item_collect_three.setVisibility(View.GONE);
        } else if (stringArr.length == 1) {
            holder.tv_item_collect_one.setVisibility(View.VISIBLE);
            holder.tv_item_collect_one.setText(stringArr[0]);
            holder.tv_item_collect_two.setVisibility(View.GONE);
            holder.tv_item_collect_three.setVisibility(View.GONE);
        } else if (stringArr.length == 2) {
            holder.tv_item_collect_one.setVisibility(View.VISIBLE);
            holder.tv_item_collect_one.setText(stringArr[0]);
            holder.tv_item_collect_two.setVisibility(View.VISIBLE);
            holder.tv_item_collect_two.setText(stringArr[1]);
            holder.tv_item_collect_three.setVisibility(View.GONE);
        } else if (stringArr.length == 3) {
            holder.tv_item_collect_one.setVisibility(View.VISIBLE);
            holder.tv_item_collect_one.setText(stringArr[0]);
            holder.tv_item_collect_two.setVisibility(View.VISIBLE);
            holder.tv_item_collect_two.setText(stringArr[1]);
            holder.tv_item_collect_three.setText(stringArr[2]);
            holder.tv_item_collect_three.setVisibility(View.VISIBLE);
        }
        if (TextUtils.isEmpty(collectList.get(i).getPosition_good())) {
            holder.tv_item_collect_content.setVisibility(View.GONE);
        } else {
            holder.tv_item_collect_content.setVisibility(View.VISIBLE);
            holder.tv_item_collect_content.setText(collectList.get(i).getPosition_good());
        }
        return view;
    }

    class ViewHolder {
        @ViewInject(R.id.iv_item_collect_delete)
        private ImageView iv_item_collect_delete;
        /**
         * 公司logo
         */
        @ViewInject(R.id.iv_item_collect_head)
        private ImageView iv_item_collect_head;
        /**
         * 职位名称
         */
        @ViewInject(R.id.tv_item_collect_name)
        private TextView tv_item_collect_name;
        /**
         * 地址/学历要求
         */
        @ViewInject(R.id.iv_item_collect_education)
        private TextView iv_item_collect_education;
        /**
         * 公司名称
         */
        @ViewInject(R.id.tv_item_collect_company_name)
        private TextView tv_item_collect_company_name;
        /**
         * 薪资待遇
         */
        @ViewInject(R.id.tv_item_collect_price)
        private TextView tv_item_collect_price;
        /**
         * 福利待遇1
         */
        @ViewInject(R.id.tv_item_collect_one)
        private TextView tv_item_collect_one;
        /**
         * 福利待遇2
         */
        @ViewInject(R.id.tv_item_collect_two)
        private TextView tv_item_collect_two;
        /**
         * 福利待遇3
         */
        @ViewInject(R.id.tv_item_collect_three)
        private TextView tv_item_collect_three;
        /**
         * 内容
         */
        @ViewInject(R.id.tv_item_collect_content)
        private TextView tv_item_collect_content;
    }
    /**
     * 自定义监听
     *
     * @param holderClickListener
     */
    public void SetOnClickListener(HolderClickListener holderClickListener) {
        this.mHolderClickListener = holderClickListener;
    }

    /**
     * 自定义监听
     */
    public interface HolderClickListener {
        void onSureHolderClick(int position);

    }
}
